import { ListCommentParm } from "@/api/comment/CommentModel";
import { nextTick, onMounted, reactive,ref } from "vue";
import { getCommentListApi,deleteCommentApi } from "@/api/comment";
import useInstance from "@/tools/useInstance";
import { ElMessage } from "element-plus";

export default function useCommentTable(){
    const {global} = useInstance()
    //表格高度
    const tableHeight = ref(0)
    //表格数据
    const tableList = ref([])
    //查询表格参数
    const listParm = reactive<ListCommentParm>({
        page: 1,
        pageSize: 10,
        totalSize: 0,
    })
    //列表
    const getList = async () => {
        let result = await getCommentListApi(listParm)
        if (result && result.code == 200) {
            //设置表格数据
            tableList.value = result.data.data
            //设置分页总条数
            listParm.totalSize = result.data.totalPage
        }
    }
    //删除
    const deleteBtn = async(commentId:string)=>{
        const flag = await global.$messageBox('确定删除该数据吗')
        if(flag){
            let result = await deleteCommentApi(commentId)
            if(result && result.code == 200){
                ElMessage.success(result.msg)
                //刷新列表
                getList()
            }
        }
    }
    //总页数改变时触发
    const sizeChange = (size: number) => {
        listParm.pageSize = size;
        getList()
    }
    //当前页数改变时触发
    const currentChange = (page: number) => {
        listParm.page = page;
        getList()
    }
    onMounted(() => {
        getList()
        //计算表格高度
        nextTick(() => {
            tableHeight.value = window.innerHeight - 435
        })
    })
    return {
        listParm,
        getList,
        deleteBtn,
        tableList,
        sizeChange,
        currentChange,
        tableHeight,
    }
}